package com.czl.module_service.adapter.asset

import android.util.Log
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.data.bean.tengyun.SortBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.ToastHelper.showNormalToast
import com.czl.module_service.R
import com.czl.module_service.databinding.ItemChooseSortListBinding
import com.czl.module_service.fragment.assetUse.SortChooseListFragment

class SortChooseListAdapter(private val fragment: SortChooseListFragment) :
    BaseQuickAdapter<SortBean, BaseDataBindingHolder<ItemChooseSortListBinding>>(
        R.layout.item_choose_sort_list
    ) {

    var sortIds: List<Int>? = null

    override fun convert(
        holder: BaseDataBindingHolder<ItemChooseSortListBinding>,
        item: SortBean,
    ) {
        holder.dataBinding?.run {
            data = item
            adapter = this@SortChooseListAdapter
            executePendingBindings()
        }
    }

    val onItemClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is SortBean) {
            var flag : Boolean = false
            Log.e("---->", sortIds.toString())
            if (sortIds?.size!! > 0) {
                sortIds!!.forEach { e ->
                    if (it.sortId!! == e) {
                        flag = true
                    }
                }
            }
            if (!flag) {
                LiveBusCenter.postAddChoosedSortEvent(it)
                fragment.back()
            } else {
                showNormalToast("该物品不可重复添加")
            }
        }
    })
}